//
//  BreakRulesVC.m
//  违章查询助手
//
//  Created by qianfeng on 15/10/27.
//  Copyright (c) 2015年 LY. All rights reserved.
//

#import "BreakRulesVC.h"
#import "HomeView.h"

#define _url @"http://apipool.37degree.com/open/?method=setting_srv.srv_list_get&did=2775bce6bbdff63b12d7a7e03d39e7ff"

#define _urlone @"http://wz.qichecdn.com/ashx/hvlocation.ashx?&cityid=-1&lat=%f&lng=%f&token=2&platform=2&version=3.2.0&qd=kqzs_cpa_test_w&_timestamp=1445602470&_sign=31310428c6e486d035b1b7ad5dcbadeb"

#define _urltwo @"http://wz.qichecdn.com/ashx/hvlocation.ashx?&cityid=-1&lat=40.122461&lng=116.257872&token=2&platform=2&version=3.2.0&qd=kqzs_cpa_test_w&_timestamp=1445602470&_sign=31310428c6e486d035b1b7ad5dcbadeb"


@interface BreakRulesVC ()<BMKMapViewDelegate,BMKLocationServiceDelegate,UITextFieldDelegate>

@end

@implementation BreakRulesVC
{
    BMKMapView * _mapView;
    BMKAnnotationView * _annotionView;
    BMKLocationService * _locationDervice;
    NSMutableArray * arr00;
    NSMutableArray * latArray;
    NSMutableArray * lngArray;
    NSMutableArray * locationArray;
    UITextField * _textField;
    UIButton * _button;
    CLPlacemark * placemark;
    float latt;
    float lont;
    int count;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    
    [self creatNav];
    [self makeMap];
    
}

-(void)getData
{
    [Fundction getWithUrl:_url success:^(id responseObject) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary * data = dic[@"data"];
        arr00 = [NSMutableArray array];
        NSArray * a0 = data[@"servers"];
        [arr00 setArray:a0];
    } failure:^(id error) {
        NSArray * a0 = @[@"39.905",@"116.44"];
        [arr00 setArray:a0];
        NSLog(@"%@",error);
    }];
    [self createData];
}

-(void)createData
{
    if (latt == 0) {
        latt = 39.905;
        lont = 116.44;
    }
    [Fundction getWithUrl:[NSString stringWithFormat:_urlone,latt,lont] success:^(id responseObject) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary * result = dic[@"result"];
        NSArray * point = result[@"points"];
        latArray = [[NSMutableArray alloc]init];
        lngArray = [[NSMutableArray alloc]init];
        locationArray = [[NSMutableArray alloc]init];
        for (NSDictionary * value in point) {
            NSString * lat = value[@"lat"];
            NSString * lng = value[@"lng"];
            NSString * location = value[@"location"];
            [latArray addObject:lat];
            [lngArray addObject:lng];
            [locationArray addObject:location];
        }
        [self makeMap];
    } failure:^(id error) {
        latArray = [[NSMutableArray alloc]init];
        lngArray = [[NSMutableArray alloc]init];
        locationArray = [[NSMutableArray alloc]init];
        NSLog(@"error == %@",error);
    }];
    
}


-(void)makeMap
{
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 64, Width, Height)];
    _mapView.delegate = self;
    _mapView.mapType = 0;
    //显示位置
    _mapView.showsUserLocation = YES;
    
    //跟着模式
    _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
    UILongPressGestureRecognizer  * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];
    longPress.minimumPressDuration = 1;
    [_mapView addGestureRecognizer:longPress];
    [self.view addSubview:_mapView];
    
    for (int i=0; i<latArray.count; i++) {
        BMKPointAnnotation * pointAnnotation = [[BMKPointAnnotation alloc]init];
        pointAnnotation.coordinate = CLLocationCoordinate2DMake([latArray[i] floatValue],[lngArray[i] floatValue]);
        pointAnnotation.title = locationArray[i];
        [_mapView addAnnotation:pointAnnotation];
    }
    if (count == 1) {
        CLLocationCoordinate2D centerLocation = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
        BMKCoordinateSpan span = BMKCoordinateSpanMake(10, 10);
        BMKCoordinateRegion regoin = BMKCoordinateRegionMake(centerLocation, span);
        [_mapView setRegion:regoin animated:YES];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }
    
    [self createButton];
    
}
-(void)longPress
{
    [_textField resignFirstResponder];
}
-(void)createButton
{
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 84, 150, 40)];
    _textField.placeholder = @"请输入城市名";
    _textField.layer.borderWidth = 2;
    _textField.layer.borderColor = [UIColor grayColor].CGColor;
    _textField.layer.cornerRadius = 10;
    _textField.layer.masksToBounds = YES;
    _textField.delegate = self;
    [self.view addSubview:_textField];
    
    _button = [[UIButton alloc]initWithFrame:CGRectMake(180, 84, 80, 40)];
    [_button setTitle:@"前往查看" forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _button.layer.cornerRadius = 10;
    _button.layer.masksToBounds = YES;
    _button.layer.borderColor = [UIColor blackColor].CGColor;
    _button.layer.borderWidth = 2;
    [self.view addSubview:_button];
    [_button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    UILabel * label = [[UILabel alloc]init];
    label.text = @"长按地图1S可以使键盘消失哦";
    label.textColor = [UIColor redColor];
    label.font = [UIFont systemFontOfSize:10];
    label.numberOfLines = 0;
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_button.mas_right).offset(5);
        make.right.equalTo(self.view.mas_right).offset(-5);
        make.top.equalTo(self.view.mas_top).offset(84);
        make.height.equalTo(@40);
    }];
}

-(void)buttonClick
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    CLGeocoder * geocode = [[CLGeocoder alloc]init];
    [geocode geocodeAddressString:_textField.text completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"查看失败了,请再次查看" delegate:self cancelButtonTitle:@"好的" otherButtonTitles: nil];
            [alert show];
        }else{
            placemark = [placemarks lastObject];
            latt = placemark.location.coordinate.latitude;
            lont = placemark.location.coordinate.longitude;
            count = 1;
            [self getData];
        }
    }];
}


-(void)creatNav
{
    UINavigationBar * bar=self.navigationController.navigationBar;
    [bar setBackgroundImage:[UIImage imageNamed:@"custom_dialog_btn_bg"] forBarMetrics:UIBarMetricsDefault];
    [self.view addSubview:bar];
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    [button setImage:[UIImage imageNamed:@"back_press"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = item;
    
    [button addTarget:self action:@selector(ReturnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
    label.text = @"违章高发地";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    self.navigationItem.titleView = label;
    
    UIButton * btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"back_press"] forState:UIControlStateNormal];
    [self.view addSubview:btn1];
    UIButton * btn2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"btn_share"] forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    UIBarButtonItem * item1 = [[UIBarButtonItem alloc]initWithCustomView:btn1];
    UIBarButtonItem * item2 = [[UIBarButtonItem alloc]initWithCustomView:btn2];
    NSArray * a1 = @[item1,item2];
    self.navigationItem.rightBarButtonItems = a1;
    [btn1 addTarget:self action:@selector(btnItemClick) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

}

-(void)ReturnClick
{
    NSArray * a1 = self.navigationController.viewControllers;

    [self.navigationController popToViewController:a1[0] animated:YES];
}


-(void)btnItemClick
{
    
}

-(void)btnClick
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
















































